﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.BLL;
using SIS.ENT;

public partial class student_PhotoUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadSchoolList();

            if (Request.Params["id"] != null)
            {

                int _accountId = 0;
                if (Int32.TryParse(Request.Params["id"].ToString(), out _accountId))
                {
                    LoadStudentInfo(_accountId);
                }
                else
                {
                    lblUserAccountKey.Text = "-1";
                }


            }


        }
    }

    private bool LoadStudentInfo(int AccountID)
    {
        bool _retValue = true;
        UserAccountTable _account = new UserAccountTable();
        _account = UserAccountTableManager.GetItem(AccountID);

        //if (_account == null)
        //{
        //    lblTopMessage.Text = "Student account ID (" + AccountID.ToString() + ") is not found!";
        //    hplStudent.Visible = false;
        //    _retValue = false;
        //}
        //else if (_account.UserAccountKey <= 0)
        //{
        //    lblTopMessage.Text = "Student account ID is not found!";
        //    hplStudent.Visible = false;
        //    _retValue = false;
        //}

        if (_account != null && _account.UserAccountKey > 0)
        {
            if (_account.UserAccountTypeCode != 300)
            {
                //lblTopMessage.Text = "Only for student account ID will be loaded.";
                //hplStudent.Visible = false;
                _retValue = false;
            }

            lblUserAccountKey.Text = _account.UserAccountKey.ToString();

            StudentProfileTable _sprofile = new StudentProfileTable();
            //StudentAddressTable _saddress = new StudentAddressTable();
            //StudentContactTable _scontact = new StudentContactTable();
            StudentGuardianRelationshipTable _gsrelation = new StudentGuardianRelationshipTable();
            //ddlSchoolList.Text = _account.SchoolKey.ToString();
            //ddlSchoolList.Enabled = false;

            _sprofile = StudentProfileTableManager.GetItemByUserAccountKey(AccountID);
            if (_sprofile != null && _sprofile.StudentProfileKey > 0)
            {
                lblStudentProfileKey.Text = _sprofile.StudentProfileKey.ToString();

                //txtFirstName.Text = _sprofile.FirstName;
                //txtLastName.Text = _sprofile.LastName;
                //txtOtherName.Text = _sprofile.OtherName;
                string _fullName = string.Empty;
                if (_sprofile.FirstName.Length > 0)
                {
                    _fullName = _sprofile.FirstName;
                }

                if (_sprofile.LastName.Length > 0)
                {
                    _fullName += " " + _sprofile.LastName;
                }

                if (_sprofile.OtherName.Length > 0)
                {
                    _fullName += " &lt;or&gt; " + _sprofile.OtherName;
                }
                //lblFullName.Text = _fullName;
                //_fullName = " - " + _sprofile.Gender + " ";

                DateTime _dtDOB = _sprofile.DateOfBirth;
                int _age = DateTime.Now.Year - _dtDOB.Year;
                _fullName += " ( " + _sprofile.Gender + ", " + _age.ToString() + " yrs )";
                lblFullName.Text = _fullName;

                if (_sprofile.Photo != null && _sprofile.Photo.Length > 0)
                {
                    imgCurrentStudentPhoto.ImageUrl = "~/images/imagehandler.ashx?key=" + _sprofile.StudentProfileKey.ToString() + "&type=studentphoto";
                    hplCurrentStudentPhoto.NavigateUrl = imgCurrentStudentPhoto.ImageUrl;
                    imgCurrentStudentPhoto.Width = 150;
                    imgCurrentStudentPhoto.Height = 200;
                }
                else
                {
                    imgCurrentStudentPhoto.ImageUrl = "";
                    hplCurrentStudentPhoto.NavigateUrl = "";
                    imgCurrentStudentPhoto.Width = 0;
                    imgCurrentStudentPhoto.Height = 0;
                }
                //ddlDOBDay.Text = _dtDOB.Day.ToString("00");
                //ddlDOBMonth.Text = _dtDOB.Month.ToString("00");
                //ddlDOBYear.Text = _dtDOB.Year.ToString("0000");

                //txtNRICNumber.Text = _sprofile.NRICNumber;
                //ddlDrivingLicenceType.Text = _sprofile.DrivingLicenceType.ToString();
                //txtDrivingLicenceNumber.Text = _sprofile.DrivingLicenseNumber;

                //ddlTravelDocType.Text = _sprofile.TravelDocumentType.ToString();
                //txtTravelDocNumber.Text = _sprofile.TravelDocumentNumber;

                //ddlBloodGroup.Text = _sprofile.BloodGroupCode.ToString();
                //ddlMaritalStatus.Text = _sprofile.MartialStatusCode.ToString();
                //ddlRaceCode.Text = _sprofile.RaceCode.ToString();
                //ddlReligion.Text = _sprofile.ReligionCode.ToString();
                //ddlNationality.Text = _sprofile.NationalityCode.ToString();
                //txtWebsiteUrl.Text = _sprofile.WebSiteUrl;
                //txtDisablityRemark.Text = _sprofile.DisabilityRemark;
                //txtRemark.Text = _sprofile.Remark;
            }
            else
            {
                _retValue = false;
            }

            //_saddress = StudentAddressTableManager.GetItemByUserAccountKey(AccountID);
            //if (_saddress != null && _saddress.StudentAddressKey > 0)
            //{
            //    //lblStudentAddressKey.Text = _saddress.StudentAddressKey.ToString();
            //    //txtUnitNo.Text = _saddress.UnitNo;
            //    //txtStreetName.Text = _saddress.StreetName;
            //    //ddlCity.Text = _saddress.CityCode.ToString();
            //    //ddlDistrict.Text = _saddress.DistrictCode.ToString();
            //    //ddlCountry.Text = _saddress.CountryCode.ToString();
            //    //txtPostCode.Text = _saddress.PostalCode;
            //}
            //else
            //{
            //    _retValue = false;
            //}

            //_scontact = StudentContactTableManager.GetItemByUserAccountKey(AccountID);
            //if (_scontact != null && _scontact.StudentContactKey > 0)
            //{
            //    string _contactInfo = "<ul>";
            //    if (_scontact.MobileNumber != null && _scontact.MobileNumber.Length > 0)
            //    {
            //        _contactInfo = "<li>Mobile: " + _scontact.MobileNumber + "<li>";
            //    }

            //    if (_scontact.HomePhoneNumber != null && _scontact.HomePhoneNumber.Length > 0)
            //    {
            //        _contactInfo += "<li>Home: " + _scontact.HomePhoneNumber + "</li>";
            //    }

            //    if (_scontact.OtherNumber != null && _scontact.OtherNumber.Length > 0)
            //    {
            //        //if (_contactInfo.Length > 0)
            //        //    _contactInfo += ", ";

            //        _contactInfo += "<li>Other: " + _scontact.OtherNumber + "</li>";
            //    }

            //    if (_scontact.EmailAddress != null && _scontact.EmailAddress.Length > 0)
            //    {
            //        //if (_contactInfo.Length > 0)
            //        //    _contactInfo += ", ";

            //        _contactInfo += "<li>Email: " + _scontact.EmailAddress + "</li>";
            //    }
            //    _contactInfo += "</ul>";

            //    lblContactInfo.Text = _contactInfo;
            //    //lblStudentContactKey.Text = _scontact.StudentContactKey.ToString();
            //    //txtMobileNumber.Text = _scontact.MobileNumber;
            //    //txtHomePhoneNumber.Text = _scontact.HomePhoneNumber;
            //    //txtEmailAddress.Text = _scontact.EmailAddress;
            //    //txtOtherContactNumber.Text = _scontact.OtherNumber;
            //}
            //else
            //{
            //    _retValue = false;
            //}

            _gsrelation = StudentGuardianRelationshipTableManager.GetItemByStudentAccountKey(AccountID, true);

            if (_gsrelation != null && _gsrelation.GuardianAccountKey > 0)
            {

                //lblStudentGuardianRelationTypeKey.Text = _gsrelation.GSAutoID.ToString();
                UserAccountTable _gaccount = new UserAccountTable();
                _gaccount = UserAccountTableManager.GetItem(_gsrelation.GuardianAccountKey);

                if (_gaccount != null && _gaccount.UserAccountKey > 0)
                {
                    //lblGuardianAccountKey.Text = _gaccount.UserAccountKey.ToString();
                    GuardianProfileTable _gprofile = new GuardianProfileTable();
                    //GuardianAddressTable _gaddress = new GuardianAddressTable();
                    //GuardianContactTable _gcontact = new GuardianContactTable();

                    _gprofile = GuardianProfileTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey);
                    if (_gprofile != null && _gprofile.GuardianProfileKey > 0)
                    {
                        string _fullName = string.Empty;
                        if (_gprofile.FirstName.Length > 0)
                        {
                            _fullName = _gprofile.FirstName;
                        }

                        if (_gprofile.LastName.Length > 0)
                        {
                            _fullName += " " + _gprofile.LastName;
                        }

                        if (_gprofile.OtherName != null && _gprofile.OtherName.Length > 0)
                        {
                            _fullName += " ( " + _gprofile.OtherName + " )";
                        }

                        //ddlGSRelation.Text = _gsrelation.RelationshipTypeCode.ToString();
                        //ddlGSRelation.Enabled = false;
                        lblGuardianFullName.Text = _fullName;
                        //lblGuardianProfileKey.Text = _gprofile.GuardianProfileKey.ToString();
                        //txtGuardianFirstName.Text = _gprofile.FirstName;
                        //txtGuardianLastName.Text = _gprofile.LastName;

                        //DateTime _dtGDOB = _gprofile.DateOfBirth;
                        //ddlGuardianDOBDay.Text = _dtGDOB.Day.ToString("00");
                        //ddlGuardianDOBMonth.Text = _dtGDOB.Month.ToString("00");
                        //ddlGuardianDOBYear.Text = _dtGDOB.Year.ToString("0000");
                        //ddlGuardianRelationshipType.Text = _gsrelation.RelationshipTypeCode.ToString();
                    }

                    //_gaddress = GuardianAddressTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey);
                    //if (_gaddress != null && _gaddress.GuardianAddressKey > 0)
                    //{
                    //    //lblGuardianAddressKey.Text = _gaddress.GuardianAddressKey.ToString();
                    //    //txtGuardianAddressUnitNo.Text = _gaddress.UnitNo;
                    //    //txtGuardianAddressStreet.Text = _gaddress.StreetName;
                    //    //txtGuardianAddressPostal.Text = _gaddress.PostalCode;
                    //    //ddlGuardianAddressCountry.Text = _gaddress.CountryCode.ToString();
                    //}

                    //_gcontact = GuardianContactTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey);
                    //if (_gcontact != null && _gcontact.GuardianContactKey > 0)
                    //{
                    //    //lblGuardianContactKey.Text = _gcontact.GuardianContactKey.ToString();
                    //    //txtGuardianHomePhoneNumber.Text = _gcontact.HomePhoneNumber;
                    //    //txtGuardianMobileNumber.Text = _gcontact.MobileNumber;
                    //    string _contactInfo = "<ul>";
                    //    if (_gcontact.MobileNumber != null && _gcontact.MobileNumber.Length > 0)
                    //    {
                    //        _contactInfo = "<li>Mobile: " + _gcontact.MobileNumber + "<li>";
                    //    }

                    //    if (_gcontact.HomePhoneNumber != null && _gcontact.HomePhoneNumber.Length > 0)
                    //    {
                    //        _contactInfo += "<li>Home: " + _gcontact.HomePhoneNumber + "</li>";
                    //    }
                    //    _contactInfo += "</ul>";
                    //    lblGuardianContactInfo.Text = _contactInfo;
                    //}
                }
                else
                {
                    _retValue = false;
                }
            }
            else
            {
                _retValue = false;
            }
        }
        return _retValue;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "Upload failed!";

        int _fileLen = 0;
        string _fileName = string.Empty;
        string _fileExt = string.Empty;

        bool _fileExtValid = false;

        string[] _arrFileExt = System.Configuration.ConfigurationManager.AppSettings["SchoolImageUploadFileExt"].Split(',');

        int _PhotoFileSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["StudentUploadLogoFileSizeKB"]);


        if (fupPhoto.HasFile && fupPhoto.PostedFile != null)
        {
            if (fupPhoto.PostedFile.ContentLength > 0)
            {
                _fileLen = fupPhoto.PostedFile.ContentLength / 1024;
            }

            _fileName = fupPhoto.PostedFile.FileName;
            _fileExt = System.IO.Path.GetExtension(_fileName).ToLower();

            for (int i = 0; i < _arrFileExt.Length; i++)
            {
                if (_fileExt == _arrFileExt[i])
                {
                    _fileExtValid = true;
                    break;
                }
            }

            if (_fileExtValid)
            {
                if (_fileLen > 0 && _fileLen <= _PhotoFileSize)
                {
                    Byte[] imgPhoto = null;
                    //To create a PostedFile
                    HttpPostedFile File = fupPhoto.PostedFile;
                    //Create byte Array with file len
                    imgPhoto = new Byte[File.ContentLength];
                    //force the control to load data in array
                    File.InputStream.Read(imgPhoto, 0, File.ContentLength);

                    if (StudentProfileTableManager.UpdatePhoto(Convert.ToInt32(lblStudentProfileKey.Text), imgPhoto))
                    {
                        lblMessage.Text = "<br/>Update photo succeeded!";
                        imgCurrentStudentPhoto.ImageUrl = "~/images/imagehandler.ashx?key=" + lblStudentProfileKey.Text + "&type=studentphoto";
                        hplCurrentStudentPhoto.NavigateUrl = imgCurrentStudentPhoto.ImageUrl;
                        imgCurrentStudentPhoto.Width = 100;
                        imgCurrentStudentPhoto.Height = 150;
                    }
                    else
                    {
                        lblMessage.Text = "<br/>Update photo failed!";
                    }
                    imgPhoto = null;
                    File = null;
                }
                else
                {
                    lblMessage.Text = "<br/>Photo size is over limit!";
                }
            }
            else
            {
                lblMessage.Text += "<br/>Photo file type is NOT allowed to update!";
            }
        }
        else
        {
            lblMessage.Text = "No photo file was uploaded!";
        }
    }

    protected void btnViewSchoolProfile_Click(object sender, EventArgs e)
    {
        Response.Redirect("profile.aspx");
    }

    protected void btnViewStudentProfile_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx?id=" + lblUserAccountKey.Text);
    }
}
